<?php

namespace App\Repositories;

use App\Exceptions\BasicException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Redis;

use function Matrix\add;

//引入hash

class AdminRechargeRepository
{
    /**
     * 添加管理员充值记录
     *
     * @param  $param
     * @return bool
     * @throws BasicException
     */
    public function addAdminRecharge($user_id, $admin_id, $type, $gold, $remark, $purpose)
    {
        $data['admin_id'] = $admin_id;
        $data['user_id'] = $user_id;
        $data['add_time'] = time();
        $data['type'] = $type;
        $data['gold'] = $gold;
        $data['remark'] = $remark;
        $data['purpose'] = $purpose;

        $newAccountLog = app('model_admin_recharge')->create($data);
        if (!$newAccountLog) {
            throw new BasicException(93132);
        }

        return $newAccountLog;
    }

    public function getAdminRechargeQueryList($params)
    {
        $start_time = isset($params['start_time']) ? strtotime($params['start_time']) : 0;
        $end_time = isset($params['end_time']) ? strtotime($params['end_time'] . " 23:59:59") : 0;
        $type = $params['type'] ?? 0;
        $purpose = $params['purpose'] ?? 0;
        $adminId = isset($params['admin_id']) ? $params['admin_id'] : '';

        $whereArr = [];
        if ($start_time) {
            $whereArr[] = ["t1.add_time", ">=", $start_time];
        }
        if ($end_time) {
            $whereArr[] = ["t1.add_time", "<=", $end_time];
        }

        if ($type) {
            $whereArr[] = ["t1.type", "=", $type];
        }
        if ($purpose) {
            $whereArr[] = ["t1.purpose", "=", $purpose];
        }
        if ($adminId) {
            $whereArr[] = ["t1.admin_id", "=", $adminId];
        }

        $page = empty($params['page']) ? 1 : $params['page'];
        $perPage = empty($params['perPage']) ? 10 : $params['perPage'];

        $adminRechargeDataModel = app('model_admin_recharge');
        $userDataModel = app('model_user_data');
        $adminModel = app('model_admin');
        $result = app('model_admin_recharge')
            ->select('t1.id', 't1.add_time', 't1.type', 't1.gold', 't1.user_id', 't1.admin_id', 't2.nickname', 't3.admin_name', 't2.user_code', 't2.head_picture', 't1.remark', 't1.purpose')
            ->from($adminRechargeDataModel->getTable() . " as t1")
            ->leftJoin(DB::raw($userDataModel->getTable() . " as t2"), "t1.user_id", "=", "t2.user_id")
            ->leftJoin(DB::raw($adminModel->getTable() . " as t3"), "t1.admin_id", "=", "t3.admin_id")
            ->where($whereArr)
            ->orderBy('t1.add_time', 'desc')
            ->paginate($perPage, ['*'], '', $page);

        $type_arr = [1 => '余额', 2 => '收益', 3 => '特殊币'];
        $purposeArr = [1 => '测试费用', 2 => '24小时奖励', 3 => '公会周奖励', 4 => '公会月奖励', 5 => '账号冲正', 6 => '其它'];
        foreach ($result as $val) {
            $val->nickname = $val->nickname ?? '';
            $val->admin_name = $val->admin_name ?? '';
            $val->add_time = date('Y-m-d H:i:s', $val->add_time);
            $val->type_name = $type_arr[$val->type];
            $val->purpose_desc = $purposeArr[$val->purpose] ?? '';
            unset($val->admin_id);
            unset($val->user_data);
            unset($val->admin);
            unset($val->purpose);
        }

        return $result;
    }

    public function getAdminGiveGiftList($params)
    {
        $whereArr = [];
        if (!empty($params['start_time'])) {
            $whereArr[] = ["t1.created_by", ">=", $params['start_time']];
        }
        if (!empty($params['end_time'])) {
            $whereArr[] = ["t1.created_by", "<=", $params['end_time']];
        }


        if (!empty($params['nickname'])) {
            $whereArr[] = ["t2.nickname", "like", "%{$params['nickname']}%"];
        }
        if (!empty($params['user_code'])) {
            $whereArr[] = ["t2.user_code", "=", $params['user_code']];
        }

        if (!empty($params['admin_id'])) {
            $whereArr[] = ["t1.admin_id", "=", $params['admin_id']];
        }
        if (!empty($params['name'])) {
            $whereArr[] = ["t4.name", "like", '%' . $params['name'] . '%'];
        }
        $page = empty($params['page']) ? 1 : $params['page'];
        $perPage = empty($params['perPage']) ? 10 : $params['perPage'];

        $adminGiveGift = app('model_admin_give_gift');
        $userDataModel = app('model_user_data');
        $adminModel = app('model_admin');
        $GiftModel = app('model_gift');
        $model = app('model_admin_give_gift')
            ->select(['t1.id', 't2.user_code', 't2.nickname', 't4.name', 't4.price', 't1.num', 't3.admin_name', 't1.remark', 't1.created_by'])
            ->from($adminGiveGift->getTable() . " as t1")
            ->leftJoin(DB::raw($userDataModel->getTable() . " as t2"), "t1.user_id", "=", "t2.user_id")
            ->leftJoin(DB::raw($adminModel->getTable() . " as t3"), "t1.admin_id", "=", "t3.admin_id")
            ->leftJoin(DB::raw($GiftModel->getTable() . " as t4"), "t1.gift_id", "=", "t4.id")
            ->where($whereArr);

        if (!empty($params['sort'])) {
            $sort = $params['sort'] == 1 ? 'asc' : 'desc';
            $model = $model->orderBy('t4.price', $sort);
        } else {
            $model = $model->orderBy('t1.created_by', 'desc');
        }
        $result = $model->paginate($perPage, ['*'], '', $page);

        return $result;
    }
}